package tas.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tas.cbo.Combos;
import tas.dao.DaoMantenimiento;
import tas.dao.impl.DaoMantenimientoImpl;
import tas.domain.Usuarios;

@WebServlet(name = "ServletCombos", urlPatterns = {"/view/admin/Combos"})
public class ServletCombos extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String autorizacion = (String) request.getSession().getAttribute("autorizacion");
        String ID = (String) request.getSession().getAttribute("ID");

        if ((ID == null) || (autorizacion == null)
                || (!autorizacion.equals("ADMIN"))
                || (!ID.equals(request.getSession().getId()))) {

            response.sendRedirect("../");

        } else {

            request.setCharacterEncoding("UTF-8");
            String accion = request.getParameter("accion");
            String result = null;
            String contentType = "text/html;charset=UTF-8";
            //
            DaoMantenimiento daoMantenimiento = new DaoMantenimientoImpl();

            if (accion == null) {
                result = "Solicitud no recibida";

            } else if (accion.equals("CBO_ANNO")) {
                Combos cbo = new Combos();
                List<Object[]> list = cbo.annio();

                if ((list != null) && (list.size() > 0)) {
                    Integer year = Calendar.getInstance().get(Calendar.YEAR);

                    StringBuilder sb = new StringBuilder();

                    for (Object[] f : list) {
                        Integer x = (Integer) f[0];
                        if (x == year) {
                            sb.append("<option value=\"").append(x).append("\" selected=\"selected\">");
                        } else {
                            sb.append("<option value=\"").append(x).append("\">");
                        }
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                }

            } else if (accion.equals("CBO_EJE")) {
                String anno = request.getParameter("anno");

                if (!anno.equals("0")) {
                    Combos cbo = new Combos();
                    Integer idusuario = ((Usuarios) request.getSession().getAttribute("usuario")).getIdusuario();
                    List<Object[]> list = cbo.unidadEjecutora(idusuario, anno);

                    if ((list != null) && (list.size() > 0)) {
                        StringBuilder sb = new StringBuilder();

                        for (Object[] f : list) {
                            sb.append("<option value=\"").append(f[0]).append("\">");
                            sb.append(f[1]);
                            sb.append("</option>");
                        }

                        result = sb.toString();
                    }
                }

            } else if (accion.equals("CBO_COR")) {
                String anno = request.getParameter("anno");
                String id = request.getParameter("ueje");

                Combos cbo = new Combos();
                Integer idusuario = ((Usuarios) request.getSession().getAttribute("usuario")).getIdusuario();
                List<Object[]> list = cbo.corte(id, idusuario, anno);

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                }

            } else if (accion.equals("CBO_TIPO")) {
                Combos cbo = new Combos();
                List<Object[]> list = cbo.tipoNoLaborable();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                }

            } //
            else if (accion.equals("CBO_DEP")) {
                String anno = request.getParameter("anno");
                String ueje = request.getParameter("ueje");
                String idcorte = request.getParameter("idcorte");

                Combos cbo = new Combos();
                Integer idusuario = ((Usuarios) request.getSession().getAttribute("usuario")).getIdusuario();
                List<Object[]> list = cbo.dependencia(idcorte, idusuario, ueje, anno);

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_REGLAB")) {
                Combos cbo = new Combos();
                // ARREGLAR EL SELECT DE: regLabCbo
                List<Object[]> list = cbo.regLabCbo();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();

                    sb.append("<option value=\"0\">Todos</option>");

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }
                    result = sb.toString();
                }

            } //
            else if (accion.equals("CBO_SED")) {
                String anno = request.getParameter("anno");
                String id = request.getParameter("id");
                String idueje = request.getParameter("idueje");

                Combos cbo = new Combos();
                Integer idusuario = ((Usuarios) request.getSession().getAttribute("usuario")).getIdusuario();

                List<Object[]> list = cbo.sede(Integer.valueOf(id), idusuario, Integer.valueOf(idueje), anno);

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");
                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_AREA")) {
                String anno = request.getParameter("anno");
                String ueje = request.getParameter("ueje");
                String idcorte = request.getParameter("idcorte");
                String iddependencia = request.getParameter("iddependencia");

                Combos cbo = new Combos();
                Integer idusuario = ((Usuarios) request.getSession().getAttribute("usuario")).getIdusuario();

                List<Object[]> list = cbo.area(idcorte, idusuario, ueje, anno, iddependencia);

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");
                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_ESTADO")) {

                Combos cbo = new Combos();
                List<Object[]> list = cbo.estado();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");

                    for (Object[] f : list) {

                        if (f[0].equals(1)) {
                            sb.append("<option value=\"").append(f[0]).append("\" selected=\"selected\" >");
                        } else {
                            sb.append("<option value=\"").append(f[0]).append("\">");
                        }

                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_PER")) {
                Combos cbo = new Combos();
                List<Object[]> list = cbo.periodosCbo();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();

                    int cont = 0;

                    for (Object[] f : list) {
                        if (cont == (list.size() - 1)) {
                            sb.append("<option value=\"").append(f[0]).append("\" selected=\"selected\" >");

                        } else {

                            sb.append("<option value=\"").append(f[0]).append("\">");
                        }
                        sb.append(f[1]);
                        sb.append("</option>");
                        ++cont;
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_TURNO")) {
                Combos cbo = new Combos();

                String idAnno = request.getParameter("idanno");
                String idUeje = request.getParameter("idueje");
                String idCorte = request.getParameter("idcorte");

                List<Object[]> list = cbo.turnosCbo(idAnno, idUeje, idCorte);

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }
            } //
            else if (accion.equals("CBO_TIPHOR")) {
                Combos cbo = new Combos();
                // String idAnno = request.getParameter("idanno");
                List<Object[]> list = cbo.horariosTipoCbo();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_EXO")) {
                Combos cbo = new Combos();
                List<Object[]> list = cbo.exoneracion();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }

            } //
            else if (accion.equals("CBO_GENERO")) {

                Combos cbo = new Combos();
                List<Object[]> list = cbo.sexo();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }
            } else if (accion.equals("CBO_EST_ATEN")) {

                Combos cbo = new Combos();
                List<Object[]> list = cbo.estaten();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Todos</option>");

                    for (Object[] f : list) {
                        
                        
                        if (f[0].equals(1)) {
                            sb.append("<option value=\"").append(f[0]).append("\" selected=\"selected\" >");
                        } else {
                            sb.append("<option value=\"").append(f[0]).append("\">");
                        }
                   
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }
            }
            
            else if (accion.equals("CBO_EST_APR_SA")) {

                Combos cbo = new Combos();
                List<Object[]> list = cbo.estadosAprobSolAus();

                if ((list != null) && (list.size() > 0)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<option value=\"0\">Seleccione</option>");

                    for (Object[] f : list) {
                        sb.append("<option value=\"").append(f[0]).append("\">");
                        sb.append(f[1]);
                        sb.append("</option>");
                    }

                    result = sb.toString();
                } else {
                    result = "<option value=\"0\">Todos</option>";
                }
            } 

            if (result == null) {
                result = "";
            }

            response.setContentType(contentType);
            PrintWriter out = response.getWriter();
            out.print(result);
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
